package handler

import (
	"ball/api/request"
	"ball/global"
	"ball/internal/dao"
	"ball/internal/model"
	"github.com/gin-gonic/gin"
	"net/http"
)

func AddVideo(c *gin.Context) {
	var req request.AddVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	video := model.Videos{
		UserId: req.UserId,
		Name:   req.Name,
		Like:   req.Like,
		People: int64(req.People),
		Gift:   int64(req.Gift),
		Img:    req.Img,
	}
	if !dao.Create(&video) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"data": video,
	})
	return
}

func ListVideo(c *gin.Context) {
	list := dao.ListVideo()

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": list,
		"msg":  "展示成功",
	})
}

func InfoVideo(c *gin.Context) {
	var req request.InfoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	info := model.Videos{}
	if !dao.GetOneById(req.Id, &info) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "详情展示失败",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": info,
		"msg":  "详情展示成功",
	})
}

func SearchVideo(c *gin.Context) {
	var req request.SearchReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	search := dao.GetUser(req.Name)
	global.DB.Where("name = ?", "%"+req.Name+"%").Limit(1).Find(&search)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": search,
		"msg":  "查询成功",
	})
}
